#include "widget.h"
#include "ui_widget.h"

#include <QLabel>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //❌️
    ui->lineEdit->setClearButtonEnabled(true);
    ui->lineEdit_2->setClearButtonEnabled(true);
    ui->lineEdit_3->setClearButtonEnabled(true);

    //设置树根节点的名称
    ui->treeWidget->setHeaderLabel("root");
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_clicked()
{
    //创建一个标签页--addTab
    //参数1：QWidget
    //参数2：指定一个text标题
    int count =ui->tabWidget->count();
    QWidget* w=new QWidget();
    ui->tabWidget->addTab(w,QString("Tab ")+QString::number(count));
    //添加一个QLabel标签
    QLabel* label=new QLabel(w);
    label->setText(QString("Tab ")+QString::number(count));
    label->resize(100,50);
    //设置新标签被选中
    ui->tabWidget->setCurrentIndex(count);

}

void Widget::on_pushButton_sub_clicked()
{
    //获取当前的标签
    int count=ui->tabWidget->currentIndex();
    //删除除标签页
    ui->tabWidget->removeTab(count);
}

void Widget::on_tabWidget_tabBarClicked(int index)
{
    ui->plainTextEdit->setPlainText(ui->tabWidget->tabText(index));
}

void Widget::on_pushButton_add_1_clicked()
{
    //获取当前的选中行
    int count = ui->listWidget->currentRow();
    if(count==-1)
    {
        ui->listWidget->addItem(ui->lineEdit->text());
    }
    else
    {
        ui->listWidget->insertItem(count,ui->lineEdit->text());
    }
}

void Widget::on_pushButton_sub_1_clicked()
{
    int count = ui->listWidget->currentRow();
    ui->listWidget->takeItem(count);
}

void Widget::on_listWidget_itemClicked(QListWidgetItem *item)
{
    ui->plainTextEdit->setPlainText(item->text());
}

void Widget::on_pushButton_add_r_clicked()
{
    //获取有多少行
    int count = ui->tableWidget->rowCount();
    //qDebug()<<count;
    //添加行row
    ui->tableWidget->insertRow(count);
    //设置列名称
    if (ui->lineEdit_2->text()==nullptr)return;
    ui->tableWidget->setVerticalHeaderItem(count,new QTableWidgetItem(ui->lineEdit_2->text()));
}

void Widget::on_pushButton_sub_r_clicked()
{
    //获取有多少行
    int count = ui->tableWidget->currentRow();
    //删除行row
    ui->tableWidget->removeRow(count);
}

void Widget::on_pushButton_add_c_clicked()
{
    //获取有多少列
    int count = ui->tableWidget->columnCount();
    //添加列column
    ui->tableWidget->insertColumn(count);
    //设置列名称
    if (ui->lineEdit_2->text()==nullptr)return;
    ui->tableWidget->setHorizontalHeaderItem(count,new QTableWidgetItem(ui->lineEdit_2->text()));
}

void Widget::on_pushButton_sub_c_clicked()
{
    //获取有多少列
    int count = ui->tableWidget->currentColumn();
    //删除列column
    ui->tableWidget->removeColumn(count);
}

void Widget::on_pushButton_clear_clicked()
{
    //方法1
    //获取有多少行
    int countrow = ui->tableWidget->rowCount();
    //获取有多少列
    int countcolumn = ui->tableWidget->columnCount();
    //qDebug()<<"countrow"<<countrow;
    //qDebug()<<"countcolumn"<<countcolumn;
    for(int i=countrow-1;i>=0;i--)
    {
        //qDebug()<<"i="<<i;
        ui->tableWidget->removeRow(i);
    }
    for(int j=countcolumn-1;j>=0;j--)
    {
        //qDebug()<<"j="<<j;
        ui->tableWidget->removeColumn(j);
    }

    //    //方法2
    //    int countrow = ui->tableWidget->rowCount();
    //    //qDebug()<<"countrow"<<countrow;
    //    while(countrow != 0)
    //    {
    //        ui->tableWidget->removeRow(countrow-1);
    //        countrow = ui->tableWidget->rowCount();
    //        //qDebug()<<"countrow"<<countrow;
    //    }
    //    int countcolumn = ui->tableWidget->columnCount();
    //    //qDebug()<<"countcolumn"<<countcolumn;
    //    while(countcolumn != 0)
    //    {
    //        ui->tableWidget->removeColumn(countcolumn-1);
    //        countcolumn = ui->tableWidget->columnCount();
    //        //qDebug()<<"countcolumn"<<countcolumn;
    //    }
}

void Widget::on_tableWidget_cellClicked(int row, int column)
{
    QTableWidgetItem* item = ui->tableWidget->item(row,column);
    if(item==nullptr)return;
    QString str=item->text();
    if(str.isEmpty())return;
    ui->plainTextEdit->setPlainText(str);
}

void Widget::on_pushButton_addtop_clicked()
{
    QString text=ui->lineEdit_3->text();
    QTreeWidgetItem *newitem=new QTreeWidgetItem();
    if(text.isEmpty())
    {
        int count =ui->treeWidget->topLevelItemCount();
        newitem->setText(0,QString::number(count));
    }
    else
    {
        newitem->setText(0,text);
    }
    ui->treeWidget->addTopLevelItem(newitem);
}

void Widget::on_pushButton_add_3_clicked()
{
    //判断名称不为空
    QString text =ui->lineEdit_3->text();
    if(text.isEmpty())return;
    //获得当前节点
    QTreeWidgetItem* currentitem=ui->treeWidget->currentItem();
    if(currentitem==nullptr)return;
    //新建节点
    QTreeWidgetItem* newitem=new QTreeWidgetItem();
    newitem->setText(0,text);
    //添加
    currentitem->addChild(newitem);
    currentitem->setExpanded(true);
}

void Widget::on_pushButton_sub_3_clicked()
{
    //获得当前节点
    QTreeWidgetItem* currentitem=ui->treeWidget->currentItem();
    if(currentitem==nullptr)return;
    //获得父节点
    QTreeWidgetItem* parentitem=  currentitem->parent();
    if(parentitem==nullptr)
    {
        //删除顶层节点
        int index=ui->treeWidget->indexOfTopLevelItem(currentitem);
        ui->treeWidget->takeTopLevelItem(index);
    }
    else
    {
        //删除普通节点
        parentitem->removeChild(currentitem);
    }
}

void Widget::on_pushButton_cleartree_clicked()
{
    ui->treeWidget->clear();
}

void Widget::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    ui->plainTextEdit->setPlainText(item->text(column));
}
